package com.augurit.wf.common.extmodel;

import java.util.ArrayList;
import java.util.List;

import com.augurit.common.base.extmodel.base.CheckTreeNode;
import com.augurit.common.sysmeta.web.form.SysMetaColumnForm;
import com.augurit.common.sysmeta.web.form.SysMetaTableForm;

public class ImportFieldCheckTreeNode extends  CheckTreeNode<ImportFieldCheckTreeNode>{
	public static String TABLE_NODE = "table_node";		//表节点
	public static String FIELD_NODE = "field_node";		//字段节点
	
	public static String SPARATOR = "_" ;
	
	private String type;
	private String value;
	private String elementName;
	
	/**
	 * 格式化导入字段check树的text属性
	 * @param comments 字段注释
	 * @param name 字段名
	 * @return
	 */
	public static String formatText(String comments, String name){
		StringBuilder str = new StringBuilder();
		str.append(comments).append("(").append(name).append(")");
		return str.toString();
	}
	
	/**
	 * 格式化导入字段check树的value属性
	 * @param templateCode
	 * @param entityName
	 * @param propertyName
	 * @return
	 */
	public static String formatValue(String templateCode, String entityName, String propertyName){
		StringBuilder str = new StringBuilder();
		str.append(templateCode).append("_").append(entityName).append("_").append(propertyName);
		return str.toString();
	}
	
	
	public static ImportFieldCheckTreeNode convertToColumnTreeNode(SysMetaTableForm form){
		ImportFieldCheckTreeNode node = null;
		if(form != null){
			node = new ImportFieldCheckTreeNode();
			node.setType(ImportFieldCheckTreeNode.TABLE_NODE);
			node.setText(ImportFieldCheckTreeNode.formatText(form.getTableChineseComments(), form.getTableName()));
			node.setChecked(false);
			node.setLeaf(false);
		}
		return node;
	}
	
	public static ImportFieldCheckTreeNode convertToColumnTreeNode(String templateCode, List<String> fieldTypeElems, SysMetaColumnForm form, String entityName){
		ImportFieldCheckTreeNode node = null;
		if(form != null){
			node = new ImportFieldCheckTreeNode();
			node.setType(ImportFieldCheckTreeNode.FIELD_NODE);
			node.setValue(ImportFieldCheckTreeNode.formatValue(templateCode, entityName, form.getPropertyName()));
			node.setText(ImportFieldCheckTreeNode.formatText(form.getColumnComments(), form.getPropertyName()));
			node.setElementName(form.getColumnComments());
			node.setLeaf(true);
			node.setChecked(false);
			
			if(fieldTypeElems != null){
				if(fieldTypeElems.contains(node.getValue()))
					node.setChecked(true);
			}
		}
		return node;
	}
	
	public static List<ImportFieldCheckTreeNode> convertTablesToColumnTreeNodeList(List<SysMetaTableForm> list){
		List<ImportFieldCheckTreeNode> nodes = null;
		if(list != null && list.size() > 0){
			nodes = new ArrayList<ImportFieldCheckTreeNode>();
			for(SysMetaTableForm form:list){
				nodes.add(convertToColumnTreeNode(form));
			}
		}
		return nodes;
	}
	
	public static List<ImportFieldCheckTreeNode> convertColumnsToColumnTreeNodeList(String templateCode, List<String> fieldTypeElems, List<SysMetaColumnForm> list, String entityName){
		List<ImportFieldCheckTreeNode> nodes = null;
		if(list != null && list.size() > 0){
			nodes = new ArrayList<ImportFieldCheckTreeNode>();
			for(SysMetaColumnForm form:list){
				nodes.add(convertToColumnTreeNode(templateCode, fieldTypeElems,form, entityName));
			}
		}
		return nodes;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getElementName() {
		return elementName;
	}

	public void setElementName(String elementName) {
		this.elementName = elementName;
	}
}
